package commons.meta.impl;

import groovy.lang.GroovyClassLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import commons.conf.ResourceApplication;
import commons.conf.ResourceDomain;
import commons.conf.xml.XmlResourceApplication;
import commons.meta.RuntimeApplication;
import commons.meta.RuntimeControllerObject;
import commons.meta.RuntimeDomainObject;
import commons.meta.RuntimeServiceObject;
import commons.meta.RuntimeViewObject;
import commons.meta.impl.hibernate.HibernateDomainGenerater;

public class DefaultRuntimeApplication implements RuntimeApplication {

	private ResourceApplication resourceApplication;

	private DomainGenerater domainGenerater = new HibernateDomainGenerater(this);

	private ViewGenerater viewGenerater;

	private ServiceGenerater serviceGenerater;

	private ControllerGenerater controllerGenerater;

	private List<RuntimeDomainObject> domains;

	private List<RuntimeViewObject> views;

	private List<RuntimeServiceObject> services;

	private List<RuntimeControllerObject> controllers;

	private List<File> hbmXmlFiles = new ArrayList<File>();

	protected void initApplication() {
		// TODO
		/*
		 * List<ResourceDomain> ds = resourceApplication.getAllDomains();
		 * domains = this.domainGenerater.generate(ds);
		 */
		compiler();
	}

	protected void compiler() {
		List<ResourceDomain> ds = resourceApplication.getAllDomains();
		domainGenerater.compiler(ds, hbmXmlFiles);
		

		Thread.currentThread().setContextClassLoader(classLoader);
		Configuration conf = new Configuration().configure("/mysql.hibernate.cfg.xml");
		// conf.addFile(xmlFile);
		for (int i = 0; i < hbmXmlFiles.size(); i++) {
			conf.addFile(hbmXmlFiles.get(i));
		}
		SessionFactory sf = conf.buildSessionFactory();

		/*
		 * Class c; try { c =
		 * classLoader.loadClass("wiscom.app.example.TestBook"); GroovyObject i
		 * = (GroovyObject)c.newInstance(); i.setProperty("id", "fff"); Map root
		 * = new HashMap(); root.put("d", i);
		 * System.out.println(FtlTemplate.getTemplate("t.ftl", root));
		 * Thread.currentThread().setContextClassLoader(classLoader); Class<?>
		 * cc = ReflectHelper.classForName("wiscom.app.example.TestBook");
		 * System.out.println(cc);
		 * 
		 * } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch
		 * (InstantiationException e) { e.printStackTrace(); } catch
		 * (IllegalAccessException e) { e.printStackTrace(); }
		 */

	}

	public DomainGenerater getDomainGenerater() {
		return domainGenerater;
	}

	public void setDomainGenerater(DomainGenerater domainGenerater) {
		this.domainGenerater = domainGenerater;
	}

	public List<RuntimeDomainObject> getAllDomains() {
		return domains;
	}

	public List<RuntimeControllerObject> getAllController() {
		return controllers;
	}

	public List<RuntimeServiceObject> getAllService() {
		return services;
	}

	public List<RuntimeViewObject> getAllViews() {
		return views;
	}

	public void setResourceApplication(ResourceApplication rapp) {
		this.resourceApplication = rapp;
		initApplication();
	}

	public ViewGenerater getViewGenerater() {
		return viewGenerater;
	}

	public void setViewGenerater(ViewGenerater viewGenerater) {
		this.viewGenerater = viewGenerater;
	}

	public ServiceGenerater getServiceGenerater() {
		return serviceGenerater;
	}

	public void setServiceGenerater(ServiceGenerater serviceGenerater) {
		this.serviceGenerater = serviceGenerater;
	}

	public ControllerGenerater getControllerGenerater() {
		return controllerGenerater;
	}

	public void setControllerGenerater(ControllerGenerater controllerGenerater) {
		this.controllerGenerater = controllerGenerater;
	}

	private GroovyClassLoader classLoader = new GroovyClassLoader();

	public GroovyClassLoader getClassLoader() {
		return classLoader;
	}

	public static void main(String[] args) throws Exception {
		XmlResourceApplication x = new XmlResourceApplication();
		x.getLocations().add("classpath:commons/conf/example.xml");
		x.afterPropertiesSet();
		DefaultRuntimeApplication app = new DefaultRuntimeApplication();
		app.setResourceApplication(x);
	}
}
